home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / games / mdlv13.zip / MDLSRC.ZIP / MDL_3D.CC < prev    next >
C/C++ Source or Header  |  1996-07-09  |  23KB  |  833 lines

  1. //
  2. // this file by brian martin 1996
  3. // brian@phyast.pitt.edu
  4. //
  5. // this is part of the source for the MedDLe quake model viewer/editor
  6. //
  7. //
  8.  
  9. #include"mdl.h"
  10. #include<grdriver.h>
  11.  
  12. // pre-calculated vertex normals needed to do shading
  13. BG_3pt vertex_normal[162]={
  14. {-0.525731,0.000000,0.850651},
  15. {-0.442863,0.238856,0.864188},
  16. {-0.295242,0.000000,0.955423},
  17. {-0.309017,0.500000,0.809017},
  18. {-0.162460,0.262866,0.951056},
  19. {0.000000,0.000000,1.000000},
  20. {0.000000,0.850651,0.525731},
  21. {-0.147621,0.716567,0.681718},
  22. {0.147621,0.716567,0.681718},
  23. {0.000000,0.525731,0.850651},
  24. {0.309017,0.500000,0.809017},
  25. {0.525731,0.000000,0.850651},
  26. {0.295242,0.000000,0.955423},
  27. {0.442863,0.238856,0.864188},
  28. {0.162460,0.262866,0.951056},
  29. {-0.681718,0.147621,0.716567},
  30. {-0.809017,0.309017,0.500000},
  31. {-0.587785,0.425325,0.688191},
  32. {-0.850651,0.525731,0.000000},
  33. {-0.864188,0.442863,0.238856},
  34. {-0.716567,0.681718,0.147621},
  35. {-0.688191,0.587785,0.425325},
  36. {-0.500000,0.809017,0.309017},
  37. {-0.238856,0.864188,0.442863},
  38. {-0.425325,0.688191,0.587785},
  39. {-0.716567,0.681718,-0.147621},
  40. {-0.500000,0.809017,-0.309017},
  41. {-0.525731,0.850651,0.000000},
  42. {0.000000,0.850651,-0.525731},
  43. {-0.238856,0.864188,-0.442863},
  44. {0.000000,0.955423,-0.295242},
  45. {-0.262866,0.951056,-0.162460},
  46. {0.000000,1.000000,0.000000},
  47. {0.000000,0.955423,0.295242},
  48. {-0.262866,0.951056,0.162460},
  49. {0.238856,0.864188,0.442863},
  50. {0.262866,0.951056,0.162460},
  51. {0.500000,0.809017,0.309017},
  52. {0.238856,0.864188,-0.442863},
  53. {0.262866,0.951056,-0.162460},
  54. {0.500000,0.809017,-0.309017},
  55. {0.850651,0.525731,0.000000},
  56. {0.716567,0.681718,0.147621},
  57. {0.716567,0.681718,-0.147621},
  58. {0.525731,0.850651,0.000000},
  59. {0.425325,0.688191,0.587785},
  60. {0.864188,0.442863,0.238856},
  61. {0.688191,0.587785,0.425325},
  62. {0.809017,0.309017,0.500000},
  63. {0.681718,0.147621,0.716567},
  64. {0.587785,0.425325,0.688191},
  65. {0.955423,0.295242,0.000000},
  66. {1.000000,0.000000,0.000000},
  67. {0.951056,0.162460,0.262866},
  68. {0.850651,-0.525731,0.000000},
  69. {0.955423,-0.295242,0.000000},
  70. {0.864188,-0.442863,0.238856},
  71. {0.951056,-0.162460,0.262866},
  72. {0.809017,-0.309017,0.500000},
  73. {0.681718,-0.147621,0.716567},
  74. {0.850651,0.000000,0.525731},
  75. {0.864188,0.442863,-0.238856},
  76. {0.809017,0.309017,-0.500000},
  77. {0.951056,0.162460,-0.262866},
  78. {0.525731,0.000000,-0.850651},
  79. {0.681718,0.147621,-0.716567},
  80. {0.681718,-0.147621,-0.716567},
  81. {0.850651,0.000000,-0.525731},
  82. {0.809017,-0.309017,-0.500000},
  83. {0.864188,-0.442863,-0.238856},
  84. {0.951056,-0.162460,-0.262866},
  85. {0.147621,0.716567,-0.681718},
  86. {0.309017,0.500000,-0.809017},
  87. {0.425325,0.688191,-0.587785},
  88. {0.442863,0.238856,-0.864188},
  89. {0.587785,0.425325,-0.688191},
  90. {0.688191,0.587785,-0.425325},
  91. {-0.147621,0.716567,-0.681718},
  92. {-0.309017,0.500000,-0.809017},
  93. {0.000000,0.525731,-0.850651},
  94. {-0.525731,0.000000,-0.850651},
  95. {-0.442863,0.238856,-0.864188},
  96. {-0.295242,0.000000,-0.955423},
  97. {-0.162460,0.262866,-0.951056},
  98. {0.000000,0.000000,-1.000000},
  99. {0.295242,0.000000,-0.955423},
  100. {0.162460,0.262866,-0.951056},
  101. {-0.442863,-0.238856,-0.864188},
  102. {-0.309017,-0.500000,-0.809017},
  103. {-0.162460,-0.262866,-0.951056},
  104. {0.000000,-0.850651,-0.525731},
  105. {-0.147621,-0.716567,-0.681718},
  106. {0.147621,-0.716567,-0.681718},
  107. {0.000000,-0.525731,-0.850651},
  108. {0.309017,-0.500000,-0.809017},
  109. {0.442863,-0.238856,-0.864188},
  110. {0.162460,-0.262866,-0.951056},
  111. {0.238856,-0.864188,-0.442863},
  112. {0.500000,-0.809017,-0.309017},
  113. {0.425325,-0.688191,-0.587785},
  114. {0.716567,-0.681718,-0.147621},
  115. {0.688191,-0.587785,-0.425325},
  116. {0.587785,-0.425325,-0.688191},
  117. {0.000000,-0.955423,-0.295242},
  118. {0.000000,-1.000000,0.000000},
  119. {0.262866,-0.951056,-0.162460},
  120. {0.000000,-0.850651,0.525731},
  121. {0.000000,-0.955423,0.295242},
  122. {0.238856,-0.864188,0.442863},
  123. {0.262866,-0.951056,0.162460},
  124. {0.500000,-0.809017,0.309017},
  125. {0.716567,-0.681718,0.147621},
  126. {0.525731,-0.850651,0.000000},
  127. {-0.238856,-0.864188,-0.442863},
  128. {-0.500000,-0.809017,-0.309017},
  129. {-0.262866,-0.951056,-0.162460},
  130. {-0.850651,-0.525731,0.000000},
  131. {-0.716567,-0.681718,-0.147621},
  132. {-0.716567,-0.681718,0.147621},
  133. {-0.525731,-0.850651,0.000000},
  134. {-0.500000,-0.809017,0.309017},
  135. {-0.238856,-0.864188,0.442863},
  136. {-0.262866,-0.951056,0.162460},
  137. {-0.864188,-0.442863,0.238856},
  138. {-0.809017,-0.309017,0.500000},
  139. {-0.688191,-0.587785,0.425325},
  140. {-0.681718,-0.147621,0.716567},
  141. {-0.442863,-0.238856,0.864188},
  142. {-0.587785,-0.425325,0.688191},
  143. {-0.309017,-0.500000,0.809017},
  144. {-0.147621,-0.716567,0.681718},
  145. {-0.425325,-0.688191,0.587785},
  146. {-0.162460,-0.262866,0.951056},
  147. {0.442863,-0.238856,0.864188},
  148. {0.162460,-0.262866,0.951056},
  149. {0.309017,-0.500000,0.809017},
  150. {0.147621,-0.716567,0.681718},
  151. {0.000000,-0.525731,0.850651},
  152. {0.425325,-0.688191,0.587785},
  153. {0.587785,-0.425325,0.688191},
  154. {0.688191,-0.587785,0.425325},
  155. {-0.955423,0.295242,0.000000},
  156. {-0.951056,0.162460,0.262866},
  157. {-1.000000,0.000000,0.000000},
  158. {-0.850651,0.000000,0.525731},
  159. {-0.955423,-0.295242,0.000000},
  160. {-0.951056,-0.162460,0.262866},
  161. {-0.864188,0.442863,-0.238856},
  162. {-0.951056,0.162460,-0.262866},
  163. {-0.809017,0.309017,-0.500000},
  164. {-0.864188,-0.442863,-0.238856},
  165. {-0.951056,-0.162460,-0.262866},
  166. {-0.809017,-0.309017,-0.500000},
  167. {-0.681718,0.147621,-0.716567},
  168. {-0.681718,-0.147621,-0.716567},
  169. {-0.850651,0.000000,-0.525731},
  170. {-0.688191,0.587785,-0.425325},
  171. {-0.587785,0.425325,-0.688191},
  172. {-0.425325,0.688191,-0.587785},
  173. {-0.425325,-0.688191,-0.587785},
  174. {-0.587785,-0.425325,-0.688191}
  175. };
  176.  
  177.  
  178. BG_Block *block;
  179. void sort(float v[], int vi[], int left, int right);
  180. void swap(float v[], int vi[], int i, int j);
  181. int frame=0;
  182. int firsttime=1;
  183. int draw_flat=0,ii=5, draw_wire=0, draw_vertices=0;
  184. int draw_gshade=0, draw_textured=0, draw_gtextured=1;
  185. int show_head=0;
  186. int back_color=0;
  187.  
  188. situation eyeball;
  189. situation model;
  190. void init_view(void);
  191.  
  192.  
  193. BG_Polygon mdlpts;
  194. GrContext *vscreen;
  195.  
  196. int mdl_3d(MDL_FILE *mddll)
  197. {
  198.  
  199.     char str[128];
  200.     byte mainloop=1;
  201.     byte cur_key=0;
  202.  
  203.     FILE *in;
  204.  
  205.     int i,j,k,l,jj,kk,ll,iii,jjj,kkk;
  206.     int zed=0;
  207.     int dumby,draw_poly;
  208.     int num_screen_pts=0;
  209.     int oldmx, oldmy;
  210.     int y_val;
  211.     int x_val;
  212.     int max_depth=128;
  213.     int update=1;
  214.     int num_screen_sides=0;
  215.     int cycle=0;
  216.     int p1,p2,p3,p4;
  217.     word usi;
  218.     word objects, blocks, sides, oblocks, osides, curblock,trackblocks;
  219.     word prev_y[BG_ScreenWidth];
  220.  
  221.     unsigned ui,uj,uk,ul;
  222.  
  223.     long unsigned map_max_x=1024;
  224.     long unsigned map_max_y=1024;
  225.  
  226.     double forward=0,upward=0,rightward=0;
  227.     double t0,t1,frames, fps;
  228.     double dT=.03;
  229.     double view_angle;
  230.     double u,v,w;
  231.     double u0,v0,w0;
  232.     double a,b,c,d;
  233.     double aa,bb,cc,dd;
  234.     double front_plane=28.0, back_plane=10000.0, plane_const;
  235.     double z_scale_factor;
  236.     double zadd=5.0;
  237.     double scale_terra=.1;
  238.     struct time t;
  239.     float ftemp;
  240.     int maxn;
  241.     int draw_2d=0;
  242.  
  243.     // to scale view
  244.     BG_Matrix scaling;
  245.     // the rotation matrices
  246.     BG_Matrix Rx,Ry,Rz;
  247.     BG_Matrix N,T,M,OTemp;
  248.     BG_Matrix M1,M2,M3;
  249.     BG_Matrix E_TM,B_TM,O_TM,Temp_TM,T_TM,TM;
  250.  
  251.     BG_3pt *framemin, *framemax;
  252.     BG_3pt eye_ptlist[5000];
  253.     float xlist[5000];
  254.     int xindexlist[5000];
  255.     BG_3pt pt1, pt2, pt3, pt4, vect1, vect2, vect3;
  256.     BG_3pt light_ray;
  257.     BG_3pt LR;     // light ray
  258.     BG_3pt MD;     // move direction
  259.     BG_3pt vn[162];
  260.  
  261.  
  262.     BG_3pt X_axis,Y_axis, Z_axis;     // move direction
  263.  
  264.     BG_ScreenCoor pt[4];
  265.  
  266.     BG_MouseRange(0,0,BG_ScreenWidth,BG_ScreenHeight);
  267.  
  268.  
  269.  
  270.     BG_ClearScreen(0);
  271.     // create a screen buffer in ram to do rendering
  272.     vscreen = GrCreateContext(BG_ScreenWidth,BG_ScreenHeight,NULL,NULL);
  273.     G_buffer = (unsigned  char *)(vscreen->gc_frame.gf_baseaddr[0]);
  274.  
  275.  
  276.     if(firsttime==1)init_view();
  277.     firsttime=0;
  278.  
  279.     X_axis.x=10.0;
  280.     X_axis.y=0.0;
  281.     X_axis.z=0.0;
  282.     Y_axis.x=0.0;
  283.     Y_axis.y=10.0;
  284.     Y_axis.z=0.0;
  285.     Z_axis.x=0.0;
  286.     Z_axis.y=0.0;
  287.     Z_axis.z=10.0;
  288.  
  289.     //Rotation Matrices
  290.     Rx=BG_CalcRx(0);
  291.     Ry=BG_CalcRy(0);
  292.     Rz=BG_CalcRz(0);
  293.  
  294.     //Scaling matrix
  295.     BG_IdentityMatrix( &scaling );
  296.     //focus=20.0; // ~79 degree total view
  297.     scaling.m[0][0]=focus*(double)(BG_ScreenHeight-1)/win_height;    //scale x
  298.     scaling.m[0][1]=(BG_ScreenWidth/2);
  299.     scaling.m[2][1]=(BG_ScreenHeight/2);
  300.     scaling.m[2][2]=-focus*(double)(BG_ScreenHeight-1)